package threadPoolExecutorDemo;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class MyThreadPoolExecutor {
    //先创建一个阻塞队列用来存储任务
    BlockingQueue<Runnable> queue= new LinkedBlockingQueue<Runnable>();
    //创建一个sumbit方法用来提交Runnable
    public void sumbit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
    //定义一个构造方法来创建线程池中的线程数
    public MyThreadPoolExecutor(int n){
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(()->{
                    try {
                        while(true){
                        Runnable task=queue.take();//从队列中取出任务
                        task.run();//执行任务
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

            });
            thread.start();
        }
    }
}

public class Demo5 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoolExecutor executor = new MyThreadPoolExecutor(5);
        for (int i = 0; i < 5; i++) {
            executor.sumbit(()->{
                System.out.println(Thread.currentThread().getName());
            });
        }
    }
}
